﻿using JUtil;
using System;
using System.Windows.Forms;
using JUtil.WinForm.Extensions;

namespace jCompoment
{
    public partial class ConnectionStringEditor : Form
    {
        public ConnectionStringEditor()
        {
            InitializeComponent();

            cbProvider.InitComboBox<DbProvider>();

            cbProvider.SetValue(default(DbProvider));
        }

        public ConnectionStringEditor(DbProvider dbProvider, string connectionString)
        {
            InitializeComponent();

            cbProvider.InitComboBox<DbProvider>();

            cbProvider.SetValue(dbProvider);

            SetUi(connectionString);
        }

        private void SetUi(string connectionString)
        {
            DbProvider provider = GetDbProvider();

            uc = null;

            switch (provider)
            {
                case DbProvider.MySql:
                    uc = new MySqlConnectionStringEditor(connectionString);
                    break;
                case DbProvider.SqlServer:
                    uc = new SqlServerConnectionStringEditor(connectionString);
                    break;
                case DbProvider.Oracle:
                    uc = new OracleConnectionStringEditor(connectionString);
                    break;
                case DbProvider.Teradata:
                    uc = new TeradataConnectionStringEditor(connectionString);
                    break;
                case DbProvider.Sqlite:
                    uc = new SqliteConnectionStringEditor(connectionString);
                    break;
                case DbProvider.OleDb:
                    uc = new OleDbConnectionStringEditor(connectionString);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            cteContainer.Controls.Clear();
            cteContainer.Controls.Add(uc);
        }

        UserControl uc = null;

        private void cbProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            DbProvider provider = GetDbProvider();

            uc = null;

            switch (provider)
            {
                case DbProvider.MySql:
                    uc = new MySqlConnectionStringEditor();
                    break;
                case DbProvider.SqlServer:
                    uc = new SqlServerConnectionStringEditor();
                    break;
                case DbProvider.Oracle:
                    uc = new OracleConnectionStringEditor();
                    break;
                case DbProvider.Teradata:
                    uc = new TeradataConnectionStringEditor();
                    break;
                case DbProvider.Sqlite:
                    uc = new SqliteConnectionStringEditor();
                    break;
                case DbProvider.OleDb:
                    uc = new OleDbConnectionStringEditor();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            cteContainer.Controls.Clear();
            cteContainer.Controls.Add(uc);
        }

        public DbProvider GetDbProvider()
        {
            return cbProvider.GetValue<DbProvider>();
        }

        public string GetConnectionString()
        {
            return uc.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            DbConnectionConfig config = new DbConnectionConfig
            {
                DbProvider = GetDbProvider(),
                ConnectionString = GetConnectionString()
            };

            if (DbUtil.GetDbObject(config).TestConnection())
                MessageBox.Show("測試連線成功");
            else
                MessageBox.Show("登入失敗", "資料庫連線錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


    }
}
